﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AgriculturalMachinery.Models;
using System.IO;

namespace AgriculturalMachinery.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        AccountRepository ar = new AccountRepository();
        public ActionResult Index()
        {
            HomeViewModel hvm =  new HomeViewModel();
            try
            {
                int id = Int32.Parse(Request.Cookies["cooUserID"].Value);
                if (id != 0)
                {
                    InnerSiteInfoRepository inr = new InnerSiteInfoRepository();
                    hvm.iq = inr.GetInnerSiteInfoByID(id);
                }
            }
            catch
            {

            }
            return View(hvm);
        }
        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Login()
        {
            string email = Request.Params["email"];
            string password = Request.Params["password"];
            Clients c = ar.FindUserByEmail(email);
            
            if (c.Password.Trim().Equals(password))
            {
                Response.Cookies["cooUserID"].Value = c.UserID.ToString();
                Response.Cookies["cooUserID"].Expires = DateTime.Now.AddMinutes(20);

                Response.Cookies["cooUserName"].Value = c.UserName;
                Response.Cookies["cooUserName"].Expires = DateTime.Now.AddMinutes(20);

                Response.Cookies["cooAvatar"].Value = c.Avatar;
                Response.Cookies["cooAvatar"].Expires = DateTime.Now.AddMinutes(20);
            }
            return RedirectToAction("index");
        }

        
        public ActionResult About()
        {
            return View();
        }

    }
}
